package com.tsll.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsll.dao.*;
import com.tsll.entity.Book;
import com.tsll.entity.Directories;
import com.tsll.service.BookService;
import com.tsll.vo.BackVo;
import com.tsll.vo.BookVo;
import com.tsll.vo.EchartsVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    public BookMapper bookMapper;

    @Autowired
    public BorrowBookMapper borrowBookMapper;

    @Autowired
    public DirectoriesMapper directoriesMapper;

    @Autowired
    public ContentMapper contentMapper;


    @Autowired
    public UserMapper userMapper;
    //获取图书信息并分页
    public Result findByPage(Integer current, Integer pageSize,BookVo bookVo) {
        //启动分页
        PageHelper.startPage(current,pageSize);
        List<Book> books = bookMapper.selectByVo(bookVo);
        //封装到PgeInfo
        PageInfo<Book> bookPageInfo = new PageInfo<Book>(books);
        return new Result(200,"查询书籍成功",bookPageInfo);
    }
    //添加图书
    public Result insert(Book book) {
        int ad = bookMapper.add(book);
        if(ad==1){
            return new Result(200,"添加成功",ad);
        }else {
            return new Result(500,"添加失败",null);
        }
    }
    //修改图书
    public Result update(Book book) {
        int edit = bookMapper.edit(book);
        if(edit==1){
            return new Result(200,"修改成功",book);
        }else {
            return new Result(500,"修改失败",null);
        }
    }
    //删除图书
    public Result delBook(Integer bookId) {
        int del = bookMapper.deleteById(bookId);
        if(del==1){
            return new Result(200,"删除成功",null);
        }else {
            return new Result(500,"删除失败",null);
        }
    }
    //查询当前流行图书信息
    public Result getpopulars() {
        return new Result(200,"查询流行图书数据成功",bookMapper.getpopular());
    }
    //获取最近半年内的流行图书
    public Result AllNewDate() {
        List<Book> books = bookMapper.selectNewDate();
        return new Result(200,"获取最新日期（最近半年内发布的）",books);
    }
//    借阅审核图书信息
    public Result getBorrow(Integer current, Integer pageSize, BookVo bookVo) {
        //启动分页
        PageHelper.startPage(current,pageSize);
        List<Book> books = bookMapper.borrow(bookVo);
        //封装到PgeInfo
        PageInfo<Book> bookPageInfo = new PageInfo<Book>(books);
        return new Result(200,"查询借阅书籍成功",bookPageInfo);
    }


    //查询图书总数
    @Override
    public Result getbookcount(BookVo bookVo) {
        int count = bookMapper.getbookcount(bookVo);
        return  new Result(200,"查询图书总数成功",count);
    }
//查询图书数量
    @Override
    public Result seleectbooks() {
        return new Result(200,"查询图书成功",bookMapper.selectbooks());
    }
//查询流行图书
    @Override
    public List<EchartsVo> getcount() {
        List<EchartsVo> getcount = bookMapper.getcount();
        return getcount;
    }





    /**
     * 根据id查询图书信息
     * @param id
     * @return
     */
    @Override
    public Book selectById(Integer id) {
        return bookMapper.selectById(id);
    }


    /**
     * 根据bookId分组查询损坏书籍的信息
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public Result selectAll(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
        List<Book> books = bookMapper.selectAll();
        PageInfo<Book> pageInfo = new PageInfo<>(books);

        return new Result(200,"查询成功",pageInfo);
    }


    @Override
    public Result selectByBookTypeId(int id) {
        List<Book> books = bookMapper.selectByBookTypeId(id);
        return new Result(200,"查询成功",books);
    }

    @Override
    public Result backBook(BackVo backVo) {
        int a=bookMapper.addBookCount(backVo.getBookId(), 1);
        int b=borrowBookMapper.updateStatus(backVo.getUserId(),backVo.getBookId());
        int c=0;
        if(backVo.getScore()<100){
            c=userMapper.updateScore(backVo.getUserId());
        }
        return new Result(200,"归还成功",null) ;
    }

    @Override
    public Result readBook(Integer id) {
        List<Directories> directories = directoriesMapper.selectByDirectoriesId(id);
        return new Result(200,"成功查询内容",directories);
    }
}
